<template>
  <div v-loading="loading" class="flex_col">

		<div class="scroll_auto flex_cell">
    <el-form id="page_order_create" ref="form" :model="form" class="standard_form_gird standard_form_gird_right"
      label-position="right" label-width="140px" style="margin-bottom: 90px;">

      <ex_header_spiler :title="$tt('label_base_info')"></ex_header_spiler>

      <el-form-item :label="$tt('label_merchant_warehouse_filter')" prop="ozonWarehouseId" style="width: 550px;">
        <div class="flex_row">
          <el-input :placeholder="$tt('label_please_input_merchant_warehouse_id')" v-model.trim="warehouse"></el-input>
          <el-button class="mg_h2" @click="pushWareHouse">{{ $tt('label_add') }}</el-button>
        </div>
        <div>
          <el-tag class="mg_r2" v-for="(tag, i) in form.ozonWarehouseId" :key="i" closable
            @close="form.ozonWarehouseId.splice(i, 1)">
            {{ tag }}
          </el-tag>
        </div>
      </el-form-item>
      <el-row>
        <el-col>
          <el-form-item :label="$tt('label_outbound_warehouse')" prop="outWarehouseId">
            <base_warehouse_select :value="outWarehouseId" :isTransit="false" @changeItem="e => {
              outWarehouseId = e.id
              outWarehouseName = e.name
              outWarehouseCode = e.code
            }" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-if="platformCode == 'WILDBERRIES'">
        <el-col>
          <el-form-item :label="$tt('label_auto_sync_order')" prop="autoSyncOrderFlag">

            <ex_radio_group_auto v-model="form.autoSyncOrderFlag" enum="YesOrNoEnum"
              @input="e => e == 'N' ? $set(form, 'autoSubmitOrderFlag', e) : null" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-if="platformCode == 'WILDBERRIES'">
        <el-col>
          <el-form-item :label="$tt('label_auto_submit_order')" prop="autoSubmitOrderFlag">
            <ex_radio_group_auto v-model="form.autoSubmitOrderFlag" enum="YesOrNoEnum"
              :disabled="form.autoSyncOrderFlag == 'N'" />
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
  </div>
    <div class="page_rela_bottom_area" >
      <el-button type="primary" @click="save">{{ $tt('label_submit') }}</el-button>
    </div>



  </div>
</template>
<script>
let default_obj = {
  openSyncOrderFlag: null,//	YesOrNoEnum 	是否开启订单同步: Y 是, N 否
  autoSyncOrderFlag: null,//	YesOrNoEnum 	是否自动同步订单: Y 是, N 否
  autoSubmitOrderFlag: null,//	YesOrNoEnum 	是否自动提交订单: Y 是, N 否
  platformCode: 'OZON',//	string 	平台编码
  customerId: null,// 	long 	客户id
  detailDTOList: [default_obj_item],//array<object> 	配置明细
  ozonWarehouseId: [],
  lastSyncOrderTime: null,//		long 	最后一次同步时间
  lastSyncCancelTime: null,//		long 	最后一次同步取消时间
  syncDayTime: null,//		long 	每天同步一次的时间
}
let default_obj_item = {
  configId: null,//	long 	配置id
  ruleName: null,//	string 	规则名称
  outboundType: null,//	string 	出库类型
  bizType: null,//	string 	业务类型：2B/2C
  warehouseId: null,//long 	仓库id
  warehouseName: null,//	string 	仓库名称
  warehouseCode: null,//	string 	仓库编码
  logisticsId: null,//		long 	物流产品id
  logisticsName: null,//	string 	物流产品名称
  logisticsCode: null,//		string 	物流产品code
  logisticsType: null,//		string 	物流类型：到仓自提/配送集货点
  configAgentId: null,//		long 	货代id
}
export default {
  mixins: [],
  components: {},
  props: {
    platformCode: {
      type: String,
      default: "OZON",
    },
  },
  data() {
    return {
      loading: false,
      warehouse: "",
      outWarehouseId: '',
      outWarehouseName: '',
      outWarehouseCode: '',
      form: _.cloneDeep(default_obj),

    };
  },
  mounted() {
    this.initData()
  },
  methods: {
    initData() {
      this.loading = true
      this.$ajax({
        url: "/srm-api/ozon/ThirdpartyOrderConfigRpc/getConfigInfo.json",
        method: 'post', data: { platformCode: this.platformCode },
        success: (res) => {
          if (res.data) {
            const { ozonWarehouseId, ...other } = res.data
            this.form = Object.assign({}, _.cloneDeep(default_obj), other)
            const item = _.head(this.form.detailDTOList)
            if (item) {
              this.outWarehouseId = item.warehouseId
              this.outWarehouseName = item.warehouseName
              this.outWarehouseCode = item.warehouseCode
            }
            if (ozonWarehouseId) {
              this.form.ozonWarehouseId = ozonWarehouseId.split(',')
            }
          } else {
            // this.$refs.form.setData( Object.assign({}, _.cloneDeep(default_obj)))
            this.form = Object.assign({}, _.cloneDeep(default_obj))
          }


        },
        complete: () => {
          this.loading = false

        }

      })
    },
    pushWareHouse(data) {
      if (!this.warehouse) return this.$message.error(this.$tt("tip_can_no_empty"))
      if (this.form.ozonWarehouseId && this.form.ozonWarehouseId.includes(this.warehouse)) return this.$message.error(this.$tt("tip_no_repeat"))
      this.form.ozonWarehouseId.push(this.warehouse)
      this.warehouse = ""
    },
    saveNet() {

      this.loading = true
      const data = _.cloneDeep(this.form)
      data.ozonWarehouseId = data.ozonWarehouseId.join(',')
      data.platformCode = this.platformCode
      const item = _.head(this.form.detailDTOList)
      if (item) {
        data.detailDTOList = [
          {
            id: item.id,
            configId: item.configId,
            warehouseId: this.outWarehouseId,
            warehouseName: this.outWarehouseName,
            warehouseCode: this.outWarehouseCode,
          }
        ]
      } else {
        data.detailDTOList = [
          {
            warehouseId: this.outWarehouseId,
            warehouseName: this.outWarehouseName,
            warehouseCode: this.outWarehouseCode,
          }
        ]
      }
      this.$ajax({
        url: "/srm-api/ozon/ThirdpartyOrderConfigRpc/saveOrderConfig.json",
        method: 'post', type: 'json',
        data,
        success: (res) => {
          this.$message({
            showClose: true,
            message: this.$tt('tip_submit_success'),
            type: 'success'
          })
          this.initData()
        },
        complete: () => {
          this.loading = false

        }

      })
    },
    save() {
      if (this.$clickFast()) return
      this.$confirm(this.$tt('是否确认保存?'), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.saveNet()
      }).catch(() => {

      });

    },
  }
};

</script>
<style></style>
